const Controller = require("egg").Controller;
const errorLog = require("../utils/errorLog");

class RoleController extends Controller {
  async getRoleMenuByRoleId() {
    try {
      const { roleId } = this.ctx.request.body;
      const role = await this.ctx.service.role.getRoleMenuByRoleId(roleId);
      if (role) {
        const resArr = role.map((item) => {
          return item.menuId;
        });

        this.ctx.body = {
          code: 200,
          data: resArr,
          msg: "获取角色列表成功",
        };
      } else {
        this.ctx.body = {
          code: 500,
          msg: "获取角色列表失败",
        };
      }
    } catch (err) {
      errorLog("RoleController----->getRoleById", err);
      this.ctx.body = {
        code: 500,
        msg: err.message || "获取角色列表失败",
      };
    }
  }
  async getRoleList() {
    try {
      const { currentPage, pageSize } = this.ctx.request.body;
      const roleList = await this.ctx.service.role.getRoleList(
        currentPage,
        pageSize
      );
      if (roleList) {
        this.ctx.body = {
          code: 200,
          data: roleList,
          msg: "获取角色列表成功",
        };
      } else {
        this.ctx.body = {
          code: 500,
          msg: "获取角色列表失败",
        };
      }
    } catch (err) {
      errorLog("RoleController----->getRoleList", err);
    }
  }

  async addRoleMenuList() {
    try {
      const { roleName, roleRoute, menuIds } = this.ctx.request.body;
      const role = await this.ctx.service.role.createRole({
        roleName,
        roleRoute,
      });
      if (role) {
        const roleId = role.id;
        const roleMenu = await this.ctx.service.roleMenu.addRoleMenu(
          roleId,
          menuIds
        );
        if (roleMenu) {
          this.ctx.body = {
            code: 200,
            msg: "添加角色菜单成功",
          };
        } else {
          this.ctx.body = {
            code: 500,
            msg: "添加角色菜单失败",
          };
        }
      } else {
        this.ctx.body = {
          code: 500,
          msg: "添加角色失败",
        };
      }
    } catch (err) {
      errorLog("RoleController----->addRoleMenuList", err);
    }
  }

  async updateRoleAndMenuList() {
    //3为超级管理员，只能自己更新自己，2为标记已删除角色，不能进行修改
    try {
      const requestRoleId = this.ctx.state.user.roleId;
      const { roleId, roleData } = this.ctx.request.body;

      if (requestRoleId !== 3 && roleId == 3) {
        this.ctx.body = {
          code: 500,
          msg: "非超级管理员不能更新该角色",
        };
        return false;
      }

      if (roleId == 2) {
        this.ctx.body = {
          code: 500,
          msg: "该角色不能更新",
        };
        return false;
      }

      const data = {
        id: roleId,
        roleName: roleData.roleName,
        roleRoute: roleData.roleRoute,
      };

      const role = await this.ctx.service.role.updateRole(data);
      if (role) {
        if (requestRoleId == roleId) {
          this.ctx.body = {
            code: 200,
            msg: "更新角色信息成功，超级管理员拥有所有菜单权限，无需修改菜单权限",
          };
          return true;
        }

        const roleMenu = await this.ctx.service.roleMenu.updateRoleMenu(
          roleId,
          roleData.menuIds
        );
        if (roleMenu) {
          this.ctx.body = {
            code: 200,
            msg: "更新角色成功",
          };
        } else {
          this.ctx.body = {
            code: 500,
            msg: "更新角色失败",
          };
        }
      } else {
        this.ctx.body = {
          code: 500,
          msg: "更新角色失败",
        };
      }
    } catch (err) {
      errorLog("RoleController----->updateRoleAndMenuList", err);
      ctx.body = {
        code: 500,
        msg: err.message || "更新角色失败",
      };
    }
  }

  async deleteRole() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    const transaction = await this.ctx.model.transaction();

    try {
      if (id == 3 || id == 2) {
        ctx.body = {
          code: 500,
          msg: "该角色不能删除",
        };
        return;
      }

      const resMenu = await ctx.service.roleMenu.deleteRoleMenu(id, {
        transaction,
      });
      if (!resMenu) {
        await transaction.rollback();
        ctx.body = {
          code: 500,
          msg: "删除角色菜单关联失败",
        };
        throw new Error("删除角色菜单关联失败");
      }

      const role = await ctx.service.role.deleteRole(id, { transaction });
      if (!role) {
        await transaction.rollback();
        ctx.body = {
          code: 500,
          msg: "删除角色失败，可能有账号关联",
        };
        throw new Error("删除角色失败，可能有账号关联");
      }

      await transaction.commit();
      ctx.body = {
        code: 200,
        msg: "删除角色成功",
      };
    } catch (err) {
      await transaction.rollback();
      errorLog("RoleController----->deleteRole", err);
      ctx.body = {
        code: 500,
        msg:
          "删除角色失败，这个角色已经被账号关联，请先切换所有关联该角色的账号：" +
          err.message,
      };
    }
  }

  async getAllRole() {
    try {
      const role = await this.ctx.service.role.getAllRole();
      if (role) {
        this.ctx.body = {
          code: 200,
          data: role,
          msg: "获取角色列表成功",
        };
      } else {
        this.ctx.body = {
          code: 500,
          msg: "获取角色列表失败",
        };
      }
    } catch (err) {
      errorLog("RoleController----->getAllRole", err);
      this.ctx.body = {
        code: 500,
        msg: err.message || "获取角色列表失败",
      };
    }
  }
}

module.exports = RoleController;
